using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using CreamX.Framework.Game.Components;
using CreamXStudio.Classes;

namespace CreamXStudio.Screens
{
    public class TilesControl:UserControl
    {

        public TilesControl()
        {
            InitializeComponent();
            AutoScroll = true;
            AutoScrollMinSize = new Size(3000, 3000);
            SetupTiles();
        }
        CreamXTileGridComponent _tileset;
        public CreamXTileGridComponent tileset
        {
            get { return _tileset; }
            set
            {
                if (value == null)
                    return;
                _tileset = value;
                if (_tileset.SceneObject.Material != null)
                    image = SessionData.Instance.ImageCache[_tileset.SceneObject.Material.Name];

                AutoScrollMinSize = new Size((int)(image.Width * scale),(int)( image.Height * scale));
                SetupTiles();
            }
        }
        Image image = null;
        private TrackBar trackBar1;
        List<RectangleF> TileRects = new List<RectangleF>();
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //base.OnPaintBackground(e);
        }
        Bitmap bitmap;
        private void SetupTiles()
        {
            TileRects.Clear();
            if (_tileset == null)
                return;
            int __tilesX = _tileset.SceneObject.Material.Texture.Width / _tileset.TileCutSizeX;
            int __tilesY = _tileset.SceneObject.Material.Texture.Height / _tileset.TileCutSizeY;
            Point pnt = new Point(0, 0);// new Point(AutoScrollPosition.X, AutoScrollPosition.Y);
            if (tileset != null)
            {
                if (image != null)
                {
                    for (int y = 0; y < __tilesX; y++)
                    {
                        for (int x = 0; x < __tilesY; x++)
                        {

                            Rectangle srcrect = new Rectangle(x * _tileset.TileCutSizeX,
                                                              y * _tileset.TileCutSizeY,
                                    _tileset.TileCutSizeX, _tileset.TileCutSizeY);

                            RectangleF __rect = new RectangleF(pnt.X, pnt.Y,
                               _tileset.TileCutSizeX * scale,
                               _tileset.TileCutSizeY * scale);
                            TileRects.Add(__rect);
                            pnt.X += (int)(srcrect.Width * scale);
                            
                        }
                        pnt.Y += (int)(_tileset.TileCutSizeY * scale);
                        pnt.X = 0;
                    }
                }
            }
        }
        protected override void OnScroll(ScrollEventArgs se)
        {
            this.Invalidate();
            base.OnScroll(se);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            SuspendLayout();
            e.Graphics.Clear(Color.Black);
            if (_tileset != null && _tileset.SceneObject.Material!=null)
            {
                int count = 0;
                Point pnt = new Point(AutoScrollPosition.X, AutoScrollPosition.Y);
                e.Graphics.DrawImage(image, new RectangleF(pnt.X,pnt.Y,image.Width*scale,image.Height*scale));
                int __tilesX = _tileset.SceneObject.Material.Texture.Width / _tileset.TileCutSizeX;
                int __tilesY = _tileset.SceneObject.Material.Texture.Height / _tileset.TileCutSizeY;
                if (tileset != null)
                {
                    if (image != null)
                    {
                        foreach (RectangleF rect in TileRects)
                        {
                            RectangleF rect1 = rect;
                            rect1.Offset(AutoScrollPosition);
                            e.Graphics.DrawRectangle(Pens.White, rect1.X, rect1.Y, rect1.Width, rect1.Height);
                            if (count == SelectedTile)
                            {
                                e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Blue), 3), rect1.X, rect1.Y, rect1.Width, rect1.Height);
                            }
                            count++;
                        }
                        //for (int y = 0; y < __tilesX; y++)
                        //{
                        //    for (int x = 0; x < __tilesY; x++)
                        //    {

                        //        Rectangle srcrect = new Rectangle(x * _tileset.TileCutSizeX,
                        //                                          y * _tileset.TileCutSizeY,
                        //                _tileset.TileCutSizeX, _tileset.TileCutSizeY);

                        //        Rectangle __rect = new Rectangle(pnt.X, pnt.Y,
                        //           (int)(_tileset.TileCutSizeX * scale),
                        //           (int)(_tileset.TileCutSizeY * scale));
                        //        TileRects.Add(__rect);
                        //        e.Graphics.DrawImage(image,
                        //           __rect,
                        //            srcrect, GraphicsUnit.Pixel);
                        //        e.Graphics.DrawRectangle(Pens.White, __rect);
                        //        if (__rect.Width > 0)
                        //        {
                        //            e.Graphics.DrawString(count.ToString(), new Font("Arial", __rect.Width / 4), Brushes.White, new Point(pnt.X + 5, pnt.Y + 5));
                        //        }
                        //        if (count == SelectedTile)
                        //        {
                        //            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Blue), 3), __rect);
                        //        }


                        //        pnt.X += (int)(srcrect.Width * scale);


                        //        count++;
                        //    }
                        //    pnt.Y += (int)(_tileset.TileCutSizeY * scale);
                        //    pnt.X = 0;
                        //}
                    }
                }
            }
            base.OnPaint(e);
            ResumeLayout();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
           
        }
        public float scale = 1;
        Timer tmr = null;
        private void TilesControl_Load(object sender, EventArgs e)
        {
            tmr = new Timer();
            tmr.Enabled = true;
            tmr.Interval = 1000;
            tmr.Tick += new EventHandler(tmr_Tick);
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            //this.Invalidate();
        }
        public int SelectedTile = -1;
        public Microsoft.Xna.Framework.Rectangle SourceRect = Microsoft.Xna.Framework.Rectangle.Empty;
        private void TilesControl_MouseClick(object sender, MouseEventArgs e)
        {

            Point pnt = new Point(e.X + (-1 * AutoScrollPosition.X), e.Y + (-1 * AutoScrollPosition.Y));
            foreach (RectangleF rect in TileRects)
            {
                if (rect.Contains(pnt))
                {
                    SelectedTile = TileRects.IndexOf(rect);
                    SourceRect = new Microsoft.Xna.Framework.Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
                    this.Invalidate();
                }
            }
        }

        private void InitializeComponent()
        {
            this.trackBar1 = new System.Windows.Forms.TrackBar();
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
            this.SuspendLayout();
            // 
            // trackBar1
            // 
            this.trackBar1.AutoSize = false;
            this.trackBar1.Location = new System.Drawing.Point(3, 3);
            this.trackBar1.Maximum = 20;
            this.trackBar1.Name = "trackBar1";
            this.trackBar1.Size = new System.Drawing.Size(101, 29);
            this.trackBar1.TabIndex = 0;
            this.trackBar1.Value = 10;
            this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll_1);
            // 
            // TilesControl
            // 
            this.Controls.Add(this.trackBar1);
            this.Name = "TilesControl";
            this.Size = new System.Drawing.Size(251, 231);
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.TilesControl_MouseClick);
            ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
            this.ResumeLayout(false);

        }

        private void trackBar1_Scroll_1(object sender, EventArgs e)
        {
            scale =1+ (float)trackBar1.Value / 20f;
            AutoScrollMinSize = new Size((int)(image.Width * scale),(int)( image.Height * scale));
            SetupTiles();
            Invalidate();
        }

        public void SetScale(float scalevalue)
        {
            scale = scalevalue;
            trackBar1.Value =(int)Microsoft.Xna.Framework.MathHelper.Clamp((int)(scale * 20f),0,trackBar1.Maximum);
            Invalidate();
        }
    }
}
